<template>
  <ych-modal
    :fullscreen="false"
    :title="title"
    :visible="visible"
    :destroyOnClose="true"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.eform.apptable.entity.tableName')">
              <a-input :placeholder="$t('m.eform.formDesign.label.qing-shu-ru-biao-ming-cheng')" v-model="queryParam.tableName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.eform.apptable.entity.dsName')">
              <select-ds v-model="queryParam.dsCode" placeholder="" :allow-clear="false"></select-ds>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('m.common.button.query') }}</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <a-table
      size="middle"
      bordered
      rowKey="tableName"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="false"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: (radio ? 'radio' : 'checkbox')}"
      @change="handleTableChange"
      :customRow="clickThenCheck"
    >
    </a-table>

  </ych-modal>
</template>

<script>

  import { getAction } from '@/api/manage'
  import SelectDs from '../tag/SelectDs'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'SelectDbTableDialog',
    components: {
      SelectDs
    },
    data () {
      return {
        title: this.$t('m.common.button.selectDbTable'),
        visible: false,
        radio: false,
        dataSource: [],
        loading: false,
        selectedRowKeys: [],
        selectionRows: [],
        queryParam: {
          dsCode: 'dataSourceDefault'
        },
        columns: [
          {
            title:this.$t('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: this.$t('m.eform.apptable.entity.tableName'),
            align: 'center',
            dataIndex: 'tableName'
          },
          {
            title: this.$t('m.eform.apptable.entity.tableDesc'),
            align: 'center',
            dataIndex: 'tableDesc'
          }
        ],
        url: {
          list: YchConstant.PREFIX_EFORM + '/manage/actAppTable/getDbTables',
        },
      }
    },
    methods: {
      loadData () {
        if (!this.queryParam.tableName || this.queryParam.tableName == '') {
          this.messageUtil.warning(this.$t('m.eform.formDesign.label.qing-shu-ru-biao-ming-cheng'))
          return
        }
        this.onClearSelected()
        this.loading = true

        getAction(this.url.list, this.queryParam).then((res) => {
          if (res.success) {
            this.dataSource = res.result
            this.dataSource.forEach(vo => vo['dsCode'] = this.queryParam.dsCode)
          } else {
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      onSelectChange (selectedRowKeys, selectionRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectionRows = selectionRows
      },
      searchReset () {
        this.onClearSelected()
        this.dataSource = []
        this.queryParam = {
          dsCode: 'dataSourceDefault'
        }
      },
      onClearSelected () {
        this.selectedRowKeys = []
        this.selectionRows = []
      },
      searchQuery () {
        this.loadData()
      },
      handleTableChange (pagination, filters, sorter) {
        this.loadData()
      },
      handleCancel () {
        this.visible = false
        this.searchReset()
        this.radio = false
      },
      handleOk () {
        if (this.selectedRowKeys.length == 0) {
          this.messageUtil.warning(this.$t('m.eform.formDesign.label.qing-xuan-ze-shu-ju'))
          return
        }
        this.$emit('selectFinished', this.selectedRowKeys, this.selectionRows)
        this.handleCancel()
      },
      clickThenCheck (record) {
        return {
          on: {
            click: () => {
              if (this.radio) {
                this.onSelectChange(record.tableName.split(','), [record])
                this.handleOk()
              } else {
                const keys = this.selectedRowKeys.filter(vo => vo != record.tableName)
                if (keys.length < this.selectedRowKeys.length) {
                  const rows = this.selectionRows.filter(vo => vo.tableName != record.tableName)
                  this.selectedRowKeys = keys
                  this.selectionRows = rows
                } else {
                  this.selectedRowKeys.push(record.tableName)
                  this.selectionRows.push(record)
                }
              }
            }
          }
        }
      }
    }
  }
</script>
